<?php

/**
 * post actions.
 *
 * @package    blogallery
 * @subpackage post
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 9301 2008-05-27 01:08:46Z dwhittle $
 */
class postActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex($request)
  {
    $this->forward('post', 'list');
  }
  
  public function executeList($request)
  {
  	$c = new Criteria();
  	$c->add(PostPeer::PUBLISHED, '1');
  	$c->addDescendingOrderByColumn(PostPeer::DATETIME);
  	
    $pager = new sfPropelPager('Post', sfConfig::get('app_max_post'));
    $pager->setCriteria($c);
    $pager->setPage($request->getParameter('page') ? $request->getParameter('page') : 1);
    $pager->init();
    
    $this->pager = $pager;
    
  	myTools::prependTitle('Blog');
  }
  
  public function executeShow($request)
  {
  	if ($request->getParameter('handle'))
  	{
  	  $this->post = PostPeer::retrieveByHandle($request->getParameter('handle'));
  	}
  	else
  	{
  	  $this->post = PostPeer::retrieveByPk($request->getParameter('id'));
  	}
  	
  	$this->forward404Unless($this->post instanceof Post);
  }
  
  public function executeCreate($request)
  {
  	$this->form = new PostForm();  		

  	$this->getResponse()->addJavascript('livepipe.js', 'last');
  	$this->getResponse()->addJavascript('tabs.js', 'last'); 
  	$this->setTemplate('edit');
  }
  
  public function executeEdit($request)
  {
  	if ($this->getUser()->hasCredential('admin'))
  	{  		
  	  $post = PostPeer::retrieveByPk($request->getParameter('id'));
  	}
  	else
  	{
  	  $post = PostPeer::retrieveByPkAndUserId($request->getParameter('id'), $this->getUser()->getId());
  	}
  	
  	$this->forward404Unless($post instanceof Post);  	  
  	
  	$this->form = new PostForm($post);
  	  		
  	$this->getResponse()->addJavascript('livepipe.js', 'last');
  	$this->getResponse()->addJavascript('tabs.js', 'last'); 
  }
  
  public function executeUpdate($request)
  {
  	if ($request->isMethod('post'))
  	{
  	  if ($request->getParameter('post[id]'))
  	  {
  	  	if ($this->getUser()->hasCredential('admin'))
  	  	{
  	  	  $post = PostPeer::retrieveByPK($request->getParameter('post[id]'));  	  		
  	  	}
  	  	else
  	  	{
  	  	  $post = PostPeer::retrieveByPkAndUserId($request->getParameter('post[id]'), $this->getUser()->getId());  	  		
  	  	}
  	  }
  	  else
  	  {
  	  	$post = new Post();
  	  }
  	  
  	  $this->forward404Unless($post instanceof Post);
  	  
  	  $form = new PostForm($post); 
  	   	  
  	  $form->bind($request->getParameter('post'), $request->getFiles('post'));
  	  
  	  if ($form->isValid())
  	  {
  	  	$file = $form->getValue('file');
  	  	
  	  	if ($file)
  	  	{
  	  	  $salt = myTools::makeRandomString();
          $filename = sha1($salt.$file->getOriginalName()).$file->getExtension($file->getOriginalExtension());
          $file->save(sfConfig::get('sf_upload_dir').DIRECTORY_SEPARATOR.$filename);
  	  	}
  	  	
        $form->save();
        //TODO change back
  	  	//return $this->redirect('@default?module=post&action=show&handle='.$form->getObject()->getHandle());
  	  	return $this->redirect('@default?module=post&action=show&id='.$form->getObject()->getId());
  	  }
  	  else
  	  {
  	  	$this->form = $form;
  	  	
  	    $this->getResponse()->addJavascript('livepipe.js', 'last');
  	    $this->getResponse()->addJavascript('tabs.js', 'last');
  	     
  	  	$this->setTemplate('edit');
  	  	return sfView::SUCCESS;
  	  }
  	}
  }
  
  public function executeDelete($request)
  {
  	$post = PostPeer::retrieveByPK($request->getParameter('id'));
  	$this->forward404Unless($post instanceof Post);
  	
  	$post->delete();
  	
    return sfView::NONE;
  }
  
  public function executeUser($request)
  {
  	if ($this->getUser()->getUsername() === $request->getParameter('username'))
  	{
  	  $user = UserPeer::retrieveByPk($this->getUser()->getId());
  	}
  	else
  	{
  	  $user = UserPeer::retrievePublicByUsername($request->getParameter('username'));
  	}
  		
  	$this->forward404Unless($user instanceof User);
  	
  	$c = new Criteria();
  	$c->add(PostPeer::PUBLISHED, '1');  	
  	$c->add(PostPeer::USER_ID, $user->getId());  	
  	$c->addDescendingOrderByColumn(PostPeer::DATETIME);
  	
    $pager = new sfPropelPager('Post', sfConfig::get('app_max_post'));
    $pager->setCriteria($c);
    $pager->setPage($request->getParameter('page') ? $request->getParameter('page') : 1);
    $pager->init();
    
    $this->pager = $pager;
    
    $this->setTemplate('list');
    return sfView::SUCCESS;
  }
  
  public function executeImage($request)
  {
  	$post = PostPeer::retrieveByPK($request->getParameter('id'));
  	$this->forward404Unless($post instanceof Post);
  	
    $sizes = array_keys(sfConfig::get('app_image_sizes'));
    
    $size = (in_array($request->getParameter('size'), $sizes)) ? $request->getParameter('size') : 'home';
  	return $this->renderPartial('post/image', array('post' => $post, 'size' => $size));
  }
}
